package com.bzsg.rightsManagementSystem.controller




import com.bzsg.rightsManagementSystem.pojo.RequestPath
import com.bzsg.rightsManagementSystem.service.RequestPathService
import com.bzsg.rightsManagementSystem.util.result.ErrorType
import com.bzsg.rightsManagementSystem.util.result.RequestResult

import com.bzsg.rightsManagementSystem.util.result.ResultUtil
import org.springframework.beans.factory.annotation.Autowired

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*

@Controller
class RequestPathController {


    @Autowired
    private lateinit var pathService: RequestPathService


    @PostMapping(value = ["api/addRequestPath"])
    @ResponseBody
    fun addRequestPath(@RequestBody requestPath: RequestPath): RequestResult<*> {
        return try {
            pathService.insert(requestPath)
            ResultUtil.success()
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @PostMapping(value = ["api/deleteRequestPathById"])
    @ResponseBody
    fun deleteRequestPath(@RequestParam pathId: Int): RequestResult<*> {
        return try {
            pathService.deleteById(pathId)
            ResultUtil.success()
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @PostMapping(value = ["api/updateRequestPath"])
    @ResponseBody
    fun updateRequestPath(@RequestBody requestPath: RequestPath): RequestResult<*> {
        return try {
            pathService.update(requestPath)
            ResultUtil.success()
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @PostMapping(value = ["api/queryRequestPath"])
    @ResponseBody
    fun queryRequestPath(): RequestResult<*> {
        return try {
            val paths = pathService.queryAll()
            ResultUtil.success(paths)
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @GetMapping(value = ["api/selectTotalRequestPath"])
    @ResponseBody
    fun selectTotalRequestPath(): RequestResult<*> {
        return try {
            ResultUtil.success(pathService.selectTotalRequestPath())
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }

    @GetMapping(value = ["api/queryAllRequestPathByLimit"])
    @ResponseBody
    fun queryAllByLimit(@RequestParam pageNow: Int, @RequestParam pageSize: Int): RequestResult<*> {
        return try {
            ResultUtil.success(pathService.queryAllByLimit(pageNow, pageSize))
        } catch (e: Exception) {
            ResultUtil.error(ErrorType.UNKNOWN)
        }
    }
}